package com.dotstone.chipism.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.EventBusEntity;
import com.dotstone.chipism.fragment.DayFragment;
import com.dotstone.chipism.fragment.MonthFragment;
import com.dotstone.chipism.fragment.WeekFragment;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ElectryStatisticActivity extends BaseActivity {
    private LinearLayout mBackL;
    private TextView mStatusBar;
    private TextView mTitleTv;
    private RadioButton rb_month;
    private RadioButton rb_week;
    private RadioButton rb_day;
    private TextView detail_tv;
    private TextView elec_tv;
    // 标签切换的Fragment
    private Fragment dayFragment, weekFragment, monthFragment,currentFragment;;


    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.back_layout:
                finish();
                break;
            case R.id.rb_month:
                rb_month.setBackgroundResource(R.drawable.left_pressed);
                rb_week.setBackgroundResource(R.drawable.middle);
                rb_day.setBackgroundResource(R.drawable.right);
                rb_month.setTextColor(Color.parseColor(  "#54CBEA"));
                rb_week.setTextColor(Color.parseColor("#E3E3E3"));
                rb_day.setTextColor(Color.parseColor("#E3E3E3"));
                detail_tv.setText("月用电详情");
                clickTab1Layout();
                break;
            case R.id.rb_week:
                rb_month.setBackgroundResource(R.drawable.left);
                rb_week.setBackgroundResource(R.drawable.midele_pressed);
                rb_day.setBackgroundResource(R.drawable.right);
                rb_month.setTextColor(Color.parseColor(  "#E3E3E3"));
                rb_week.setTextColor(Color.parseColor("#54CBEA"));
                rb_day.setTextColor(Color.parseColor("#E3E3E3"));
                detail_tv.setText("周用电详情");
                clickTab2Layout();

                break;
            case R.id.rb_day:
                rb_month.setBackgroundResource(R.drawable.left);
                rb_week.setBackgroundResource(R.drawable.middle);
                rb_day.setBackgroundResource(R.drawable.right_pressed);
                rb_month.setTextColor(Color.parseColor(  "#E3E3E3"));
                rb_week.setTextColor(Color.parseColor("#E3E3E3"));
                rb_day.setTextColor(Color.parseColor("#54CBEA"));
                detail_tv.setText("日用电详情");
                clickTab3Layout();
                break;
            default:
                break;
        }
    }



    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_electry_statistic;
    }

    @Override
    public void initView(View view) {
        mBackL = $(R.id.back_layout);
        mStatusBar = $(R.id.tv_statusBar_mian);
        mTitleTv = $(R.id.title_tv);
        rb_month = $(R.id.rb_month);
        rb_week = $(R.id.rb_week);
        rb_day = $(R.id.rb_day);
        detail_tv = $(R.id.detail_tv);
        elec_tv = $(R.id.elec_tv);
        mTitleTv.setVisibility(View.VISIBLE);
        mTitleTv.setText("电量统计");
        rb_month.setBackgroundResource(R.drawable.left_pressed);
        rb_week.setBackgroundResource(R.drawable.middle);
        rb_day.setBackgroundResource(R.drawable.right);
        rb_month.setTextColor(Color.parseColor(  "#54CBEA"));
        rb_week.setTextColor(Color.parseColor("#E3E3E3"));
        rb_day.setTextColor(Color.parseColor("#E3E3E3"));
        mBackL.setOnClickListener(this);
        rb_month.setOnClickListener(this);
        rb_week.setOnClickListener(this);
        rb_day.setOnClickListener(this);
        EventBus.getDefault().register(this);
        initTab();
        boolVersion();
    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {

    }


    public void boolVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
             getWindow().addFlags(
             WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            // 改变titlebar的高度
            int statusbarHeight = BarUtils.getStatusBarHeight(this);
            RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
            layoutParams1.height = statusbarHeight;
            mStatusBar.setLayoutParams(layoutParams1);
            mStatusBar.setVisibility(View.VISIBLE);
        } else {
            mStatusBar.setVisibility(View.GONE);
        }
    }


    /**
     * 初始化底部标签
     */

    public void initTab() {
        if (monthFragment == null) {
            monthFragment = new MonthFragment();
        }
        if (!monthFragment.isAdded()) {
            // 提交事务
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.content_layout,monthFragment).commit();
            // 记录当前Fragment
            currentFragment = monthFragment;
        }
    }




    /**
     * 点击第1个tab
     */

    private void clickTab1Layout() {
        if (monthFragment == null) {
            monthFragment = new MonthFragment();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(),
                monthFragment);

    }
    /**
     * 点击第二个tab
     */

    private void clickTab2Layout() {
        if (weekFragment == null) {
            weekFragment = new WeekFragment();
            Bundle bundle = new Bundle();
          //  bundle.putInt("type", type);
          //  deviceFragment.setArguments(bundle);
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(),
                weekFragment);

    }

    /**
     * 点击第3个tab
     */

    private void clickTab3Layout() {
        if (dayFragment == null) {
            dayFragment = new DayFragment();
        }
        addOrShowFragment(getSupportFragmentManager().beginTransaction(),
                dayFragment);

    }



    /**
     * 添加或者显示碎片
     *
     * @param transaction
     * @param fragment
     */

    private void addOrShowFragment(FragmentTransaction transaction,
                                   Fragment fragment) {
        if (currentFragment == fragment)
            return;
        if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
            transaction.hide(currentFragment)
                    .add(R.id.content_layout, fragment).commit();
        } else {
            transaction.hide(currentFragment).show(fragment).commit();
        }

        currentFragment = fragment;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void getEventBusMessage(EventBusEntity entity) {
        switch (entity.getOption()) {
            case "value":
                elec_tv.setText(entity.getData());
                break;

            default:
                break;
        }
    }
}
